package com.maila.xiaoaobusiness.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.maila.xiaoaobusiness.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mark on 2017/12/6.
 */

public class AlertUtil {

    public interface  Alertlinstner{
        void clickConfirm();
        void clickSingle(int position);
    }
    public static void ShowDialog(Context context, String title, String Content, final Alertlinstner alertlinstner){
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(Content)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertlinstner.clickConfirm();
                        dialog.dismiss();
                    }
                })
                .create();
        dialog.show();

// 在dialog执行show之后才能来设置
//        TextView tvMsg = (TextView) dialog.findViewById(android.R.id.message);
//        tvMsg.setTextSize(16);
//        tvMsg.setTextColor(Color.parseColor("#4E4E4E"));
//
//        dialog.getButton(dialog.BUTTON_NEGATIVE).setTextSize(16);
//        dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("#8C8C8C"));
//        dialog.getButton(dialog.BUTTON_POSITIVE).setTextSize(16);
//        dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#1DA6DD"));
//
//        try {
//            Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
//            mAlert.setAccessible(true);
//            Object alertController = mAlert.get(dialog);
//
//            Field mTitleView = alertController.getClass().getDeclaredField("mTitleView");
//            mTitleView.setAccessible(true);
//
//            TextView tvTitle = (TextView) mTitleView.get(alertController);
//            if (null != tvTitle) {
//                tvTitle.setTextSize(16);
//                tvTitle.setTextColor(Color.parseColor("#000000"));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }


    public  static void showSingleChoiceDialog(final Context context,  String title, List<String> classes, final Alertlinstner alertlinstner) {
        AlertDialog.Builder  builder=new AlertDialog.Builder(context);
        builder.setIcon(R.mipmap.xiaoao);
        builder.setTitle(title);

        /**
         * 设置内容区域为单选列表项
         */

        final String[] items= (String[]) classes.toArray();
        builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(context, "You clicked "+items[i], Toast.LENGTH_SHORT).show();
                alertlinstner.clickSingle(i);
            }
        });

        builder.setCancelable(true);
        AlertDialog dialog=builder.create();
        dialog.show();
    }

    private void showMultiChoiceDialog(final Context context,  String title, List<String> classes, final Alertlinstner alertlinstner) {
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setIcon(R.mipmap.xiaoao);
        builder.setTitle("");

        /**
         * 设置内容区域为多选列表项
         */
        final String[] items={"周一","周二","周三","周四","周五","周六","周日"};
        final boolean[] checkitems=new boolean[]{false, true, true,true,true,true,true};
        builder.setMultiChoiceItems(items, checkitems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                checkitems[i]=b;
            }
        });
        builder.setNegativeButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               for(int i=0;i<items.length;i++){

               }
            }
        });
        builder.setCancelable(true);
        AlertDialog dialog=builder.create();
        dialog.show();

    }
}
